//
//  ChooseAddressController.m
//  ios-supplier
//
//  Created by Benlai on 15/8/3.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//

#import "ChooseAddressController.h"
#import "AddNewAddressController.h"

@interface ChooseAddressController ()<UIGestureRecognizerDelegate, AddNewAddressDelegate,UITableViewDelegate>
@property (nonatomic, strong) UITableView *addressTable;
@property (nonatomic, strong) NSArray *addressArray;
@property (nonatomic, strong) JFBStoreInfoDS *storeInfo;
@property (nonatomic, assign) NSInteger selectedID;
@end

@implementation ChooseAddressController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setNavigationItem];
    
    self.selectedID = [self.delegate getStoreInfo].ID;
    
    self.addressTable = [[UITableView alloc]initWithFrame: CGRectMake(0, TopSizeFix, self.view.frame.size.width, self.view.frame.size.height - TopSizeFix) style: UITableViewStyleGrouped];
    self.addressTable.delegate = self;
    self.addressTable.dataSource = self;
    self.addressTable.sectionFooterHeight = 0;
    [self.addressTable setSeparatorStyle: UITableViewCellSeparatorStyleNone];
    
    [self.addressTable registerNib: [UINib nibWithNibName: @"AddressCell" bundle:nil] forCellReuseIdentifier: @"addressCell"];
    [self.view addSubview: self.addressTable];
    
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    
    [self loadData];

    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    [self.navigationItem setLeftBarButtonItem: [self createBackView]];
}

- (UIBarButtonItem *)createBackView
{
    UIButton *leftButtonItem = [UIButton buttonWithType:UIButtonTypeCustom];
    
    leftButtonItem.frame = CGRectMake(0, 0, 11, 20);
    
    [leftButtonItem setBackgroundImage:[UIImage imageNamed:@"icon-Back"] forState:UIControlStateNormal];
    
    [leftButtonItem setImageEdgeInsets: UIEdgeInsetsMake((19 - 11) / 2, 0, (19 - 11) / 2, 0)];
    
    [leftButtonItem addTarget:self action:@selector(leftButtonItemClicked) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:leftButtonItem];
    
    return item;
}

-(void)leftButtonItemClicked
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)setNavigationItem
{
    self.navigationItem.title = @"选择收货地址";
    UIBarButtonItem *addNew = [[UIBarButtonItem alloc]initWithTitle: @"新增" style: UIBarButtonItemStyleBordered target: self action: @selector(addNewAddressBtnPressed)];
    self.navigationItem.rightBarButtonItem = addNew;
}

-(void)loadData
{
    [SelecctAddressDS loadAddressList:^(NSArray *addressList, NSError *error) {
        self.addressArray = addressList;
        [self storeInfoSet];
    }];
}

-(void)storeInfoSet
{
    JFBGlobalSetting * setting = [JFBGlobalSetting getSetting];
    [JFBStoreInfoDS loadStoreInfo:^(JFBStoreInfoDS *storeInfo, NSError *error) {
        self.storeInfo = storeInfo;
        [self.addressTable reloadData];
    } storeID:setting.shopId];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0)
    {
        return 1;
    }
    return self.addressArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return @"默认地址";
    }
    else{
        return @"其他地址";
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *indetifier = @"addressCell";
    AddressCell *cell = [tableView dequeueReusableCellWithIdentifier: indetifier];
    if (self.addressArray)
    {
        if (indexPath.section == 0)
        {
            cell.receiverNameLabel.text = self.storeInfo.masterName;
            cell.receiverPhoneLabel.text = self.storeInfo.masterMobile;
            cell.addressNameLabel.text = self.storeInfo.storeAddress;
            cell.addressSelectBtn.selected = self.selectedID == NSNotFound ? YES : NO;
        }
        else
        {
            SelecctAddressDS *addressInfo = self.addressArray[indexPath.row];
            cell.receiverNameLabel.text = addressInfo.receiver;
            cell.receiverPhoneLabel.text = addressInfo.receiver_phone;
            cell.addressNameLabel.text = [NSString stringWithFormat: @"%@%@" ,addressInfo.address_name ,addressInfo.address_detail];
            cell.addressSelectBtn.selected = self.selectedID == addressInfo.ID ? YES : NO;
        }
    }
    cell.delegate = self;
    [cell setSelectionStyle: UITableViewCellSelectionStyleNone];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return;
    }
    UIStoryboard *board = [UIStoryboard storyboardWithName: @"StoreManage" bundle: nil];
    AddNewAddressController *addNewAddress = (AddNewAddressController *)[board instantiateViewControllerWithIdentifier: @"addNewAddressController"];
    addNewAddress.delegate = self;
    addNewAddress.addressInfo = self.addressArray[indexPath.row];
    [self.navigationController pushViewController:addNewAddress animated: YES];
}

-(void)addNewAddressBtnPressed
{
    UIStoryboard *board = [UIStoryboard storyboardWithName: @"StoreManage" bundle: nil];
    AddNewAddressController *addNewAddress = (AddNewAddressController *)[board instantiateViewControllerWithIdentifier: @"addNewAddressController"];
    addNewAddress.delegate = self;
    [self.navigationController pushViewController:addNewAddress animated: YES];
}


-(void)didChooseAddressCell: (UITableViewCell *)cell
{
    NSIndexPath *selectIndexPath = [self.addressTable indexPathForCell: cell];
    
    if (selectIndexPath.section == 0)
    {
        [self.delegate setChooseAddress:nil];
    }
    else
    {
        [self.delegate setChooseAddress: self.addressArray[selectIndexPath.row]];
    }
    [self.navigationController popViewControllerAnimated: YES];
}

-(void)sendAddressInfo:(SelecctAddressDS *)addressInfo
{
    [self.delegate setChooseAddress: addressInfo];
}





@end
